// miniprogram/pages/edit_article/index.js;
const randomId = () => {
  let str = "0147852963qwertyuioplkjhgfdsazxcvbnm";
  let id = "";
  for (let i = 0; i < 10; i++) {
    let index = Math.floor(Math.random() * str.length);
    id += str[index];
  }
  return id;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      name: "",
      intro:"",
      imgs:[],
      foods: [],
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event){
    console.log(event);
    let name = event.target.dataset.name;
    console.log(name,event.detail);
    let form = this.data.form;
    form[name] = event.detail;
    this.setData({form});
  },
  onChangeList(event){
    let form = this.data.form;
    let index = event.target.dataset.index;
    form.foods[index].name = event.detail;
    this.setData({form});
  },
  // 添加菜谱
  addFood(){
    let form = this.data.form;
    form.foods.push({
      url: "",
      name: "",
      id: randomId()
    });
    this.setData({form})
  },

  // 删除菜谱
  deleteFood(event){
    let item = event.target.dataset.item;
    let form = this.data.form;
    let index = form.foods.findIndex(el=>el.id === item.id);
    form.foods.splice(index,1);
    this.setData({form});
  },
  // 上传图片到云
  afterRead(event) {
    console.log(event);
    console.log(event.detail);
    let item = event.target.dataset.item;
    wx.cloud.uploadFile({
      cloudPath:item.id,
      filePath: event.detail.file.url,
    }).then(res=>{
      let form = this.data.form;
      let aim = form.foods.find(el=>el.id === item.id);
      aim.url = res.fileID;
      this.setData({
        form
      })
    })
  },
  afterReadList(event){
    console.log(event,'event');

    wx.cloud.uploadFile({
      cloudPath:randomId(),
      filePath: event.detail.file.url,
    }).then(res=>{
      let form = this.data.form;
      form.imgs.push({_url:res.fileID,url:event.detail.file.url});
      this.setData({form})

    })
  },
  // 删除图片
  deleteImgs(event){
    let index = event.detail.index;
    let form = this.data.form;
    form.imgs.splice(index,1);
    this.setData({form});
  },
  // 提交数据
  submit(){
    console.log(this.data.form);
    if(!this.data.form.name){
      return
    }
    let type = randomId();
    console.log(JSON.stringify({...this.data.form,type}));
    wx.showLoading({
      mask:true,
    });
    wx.cloud.callFunction({
      name:"addHomeList",
      data:{...this.data.form,type}
    }).then(res=>{
      wx.switchTab({
        url: '/pages/home/home',
      })
      wx.showToast({
        title: res.result.msg,
      })
    })
  }
})